# -*- coding: utf-8 -*-
# class Employee < ActiveRecord::Base
#   extend EmployeeRepositiory
#   has_many :employee_positions, class_name: 'Employee::Position'
#   has_many :positions, through: :employee_positions
#   has_many :organizations, through: :employee_positions
#   has_many :employee_certificates, class_name: 'Employee::Certificate'
#   has_many :certificates, through: :employee_certificates
#   has_many :contracts
# end

# 用户模型
class Employee
  extend EmployeeRepositiory
  # include MongoId
  # self.site = Settings.server_address
  # self.element_name = "user"


  class << self
    def all(page=1)
      JSON.parse(RestClient.get Settings.roster_address + "/users?page=#{page}")["items"]
    end

    def find(id)
      result = JSON.parse(RestClient.get Settings.roster_address + "/users/#{id}")
      result.merge({certificates: certificates(result[:id])}).
        merge({contracts: contracts(result[:id])})
    end

    def employee_certificates(id)
      EmployeeCertificate.where(employee_id: id).to_a
    end

    def certificates(id)
      employee_certificates(id).map(&:certificate)
    end

    def contracts(id)
      Contract.where(employee_id: id)
    end

  end
end
